﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    public partial class MethodsWindow : Form
    {
        List<ParameterData> parameters;
        Boolean modify = false;
       
        public MethodsWindow()
        {
            InitializeComponent();
            listBoxReturnType.DataSource = DataType.types;
            listBoxVisibility.DataSource = VisibilityType.GetTypes();
            parameters = new List<ParameterData>();
        }

        public MethodsWindow(MethodData met):this()
        {
            modify = true;
            textBoxName.Text = met.GetName();
            for (int i = 0; i < met.GetParameters().Count; i++)
            {
                parameters.Add(met.GetParameters()[i]);
                listBoxParameters.Items.Add(met.GetParameters()[i].GetUMLRepresentation());
            }
            listBoxReturnType.Text = met.GetReturnType();
            listBoxVisibility.Text = met.GetVisibility();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAddParameter_Click(object sender, EventArgs e)
        {
            ParametersWindow addParam = new ParametersWindow();
            addParam.Owner = this;
            addParam.Show(this);
        }

        public void AddParameter(string name, string type)
        {
            ParameterData parameter = new ParameterData(name, type);
            parameters.Add(parameter);
            listBoxParameters.Items.Add(parameter.GetUMLRepresentation());
        }

        public void ModifyParameter(string name, string type)
        {
            int index = listBoxParameters.SelectedIndex;
            ParameterData parameter = new ParameterData(name, type);
            parameters[index] = parameter;
            listBoxParameters.Items[index]=parameters[index].GetUMLRepresentation();
        }

        private void btnEditParameter(object sender, EventArgs e)
        {
            if (listBoxParameters.SelectedItem != null)
            {
                int index = listBoxParameters.SelectedIndex;
                ParametersWindow editParam = new ParametersWindow(parameters[index]);
                editParam.Owner = this;
                editParam.Show(this);
            }
        }


        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (!modify)
            {
                WindowsManager.AddMethodToForm(listBoxVisibility.Text, textBoxName.Text, parameters, listBoxReturnType.Text);
            }
            else
            {
                WindowsManager.ModifyMethod(listBoxVisibility.Text, textBoxName.Text, parameters, listBoxReturnType.Text);
            }
            
            Close();
        }

        private void btnDeleteParameter_Click(object sender, EventArgs e)
        {
            if (listBoxParameters.SelectedItem != null)
            {
                for (int i = 0; i < parameters.Count; i++)
                {
                    if (parameters[i].GetUMLRepresentation() == listBoxParameters.Text)
                    {
                        parameters.Remove(parameters[i]);
                    }
                }

                listBoxParameters.Items.Remove(listBoxParameters.SelectedItem);
            }
        }
       
    }
}
